/**
 * Created by zy on 2016/7/7.
 * 新建 - 非内检缺陷管理
 */


;(function($, window, document, undefined){

    // 管道
    var $pipeline = $('#JQ_pipeline').select3({
        search: false
    });
    // 缺陷编号
    var $code = $('#JQ_code');
    // 缺陷类型
    var $class = $('#JQ_class').select3({search:false});
    // 缺陷子类型
    var $type = $('#JQ_type').select3({search:false});
    // 缺陷来源
    var $source = $('#JQ_source').select3({search:false});
    // 修复方法
    var repair_data = function(){
        var arr = [];
        var temp = $.trim($('#JQ_repair_hidden_data').html());
        if(temp){
            temp = temp.substr(0,temp.lastIndexOf(','));
            temp = '[' + temp + ']';
            temp = eval('('+temp+')');
            arr = temp;
        }
        return arr;
    }();
    var $repair = $('#JQ_repair').select3({
        search:false,
        data: repair_data
    });
    // 发现时间
    var $date = $('#JQ_date');
    $.datetimepicker.setLocale('zh');
    $date.datetimepicker({
        timepicker: false,
        format: 'Y-m-d',
        scrollInput: false,
        maxDate: new Date()
    });
    // 时钟位置
    var $clock = $('#JQ_clock');
    $clock.timePicker({
        title: '选择时钟方位'
    });
    // 填充缺陷类型
    DyyBase.ajax(defectApi.url_defect_class(),{},function(dataset){
        var json = DyyBase.getSelect(dataset);
        $class.add(json);
    });
    // 填充缺陷子类型
    $class.element.on('select',function(e,v){
        var id = v || $class.val();
        if(id){
            DyyBase.ajax(defectApi.url_defect_type(), {parentId:id}, function(dataset){
                var json = DyyBase.getSelect(dataset);
                $type.asset(json);
            });
        }else{
            $type.add([],false);
        }
    });
    // 填充缺陷来源
    DyyBase.ajax(defectApi.url_defect_source(),{},function(dataset){
        var json = DyyBase.getSelect(dataset);
        $source.add(json);
    });

    // 新建对象
    var filter = {
        // 获得新建项
        getCondition: function(){
            var data = {};
            data.mode = true;
            // 管道id
            data.pipeLineId = function(){
                var v = $pipeline.val();
                if(v){
                    return v;
                }else{
                    $pipeline.element.TIP({content:'管道名称不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            // 编号
            data.customNumber = function(){
                var v = $code.val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 缺陷类型
            data.defectType = function(){
                var v = $class.val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 缺陷子类型
            data.featureSubtype = function(){
                var v = $type.val();
                if(v){
                    return v;
                }else{
                    $type.element.TIP({content:'缺陷子类型不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            // 缺陷来源
            data.defectSource = function(){
                var v = $source.val();
                if(v){
                    return v;
                }else{
                    $source.element.TIP({content:'缺陷来源不能为空！'});
                    data.mode = false;
                }
                return '';
            }();
            // 绝对距离
            data.mileage = function(){
                var $v = $('#JQ_enterMileage'), v = $v.val();
                if(v){
                    if(isNaN(v)){
                        $v.TIP({content:'绝对距离须为数字！'});
                        data.mode = false;
                    }else{
                        if(v < 0){
                            $v.TIP({content:'绝对距离须为正数！'});
                            data.mode = false;
                        }
                    }
                    return v;
                }else{
                    $v.TIP({content:'绝对距离不能为空！'});
                    data.mode = false;
                }
                return '';
            }();
            // 坐标位置(x)
            data.x = function(){
                var $v = $('#JQ_x'), v = $v.val();
                if(v){
                    if(isNaN(v)){
                        $v.TIP({content:'坐标位置(x)须为数字！'});
                        data.mode = false;
                    }
                    return v;
                }else{
                    $v.TIP({content:'坐标位置(x)不能为空！'});
                    data.mode = false;
                }
                return '';
            }();
            // 坐标位置(y)
            data.y = function(){
                var $v = $('#JQ_y'), v = $v.val();
                if(v){
                    if(isNaN(v)){
                        $v.TIP({content:'坐标位置(y)须为数字！'});
                        data.mode = false;
                    }
                    return v;
                }else{
                    $v.TIP({content:'坐标位置(y)不能为空！'});
                    data.mode = false;
                }
                return '';
            }();
            // 坐标位置(z)
            data.z = function(){
                var $v = $('#JQ_z'), v = $v.val();
                if(v){
                    if(isNaN(v)){
                        $v.TIP({content:'坐标位置(z)须为数字！'});
                        data.mode = false;
                    }
                    return v;
                }else{
                    $v.TIP({content:'坐标位置(z)不能为空！'});
                    data.mode = false;
                }
                return '';
            }();
            // 缺陷尺寸(长)
            data.length = function(){
                var $v = $('#JQ_length'), v = $v.val();
                if(v){
                    if(isNaN(v)){
                        $v.TIP({content:'缺陷尺寸(长)须为数字！'});
                        data.mode = false;
                    }
                    return v;
                }
                return '';
            }();
            // 缺陷尺寸(宽)
            data.width = function(){
                var $v = $('#JQ_width'), v = $v.val();
                if(v){
                    if(isNaN(v)){
                        $v.TIP({content:'缺陷尺寸(宽)须为数字！'});
                        data.mode = false;
                    }
                    return v;
                }
                return '';
            }();
            // 深度
            data.depth = function(){
                var $v = $('#JQ_depth'), v = $v.val();
                if(v){
                    if(isNaN(v)){
                        $v.TIP({content:'深度须为数字！'});
                        data.mode = false;
                    }else{
                        if(v > 10000000 || v < 0){
                            $v.TIP({content:'深度范围 0-10000000！'});
                            data.mode = false;
                        }
                    }
                    return v;
                }
                return '';
            }();
            // 埋深
            data.buriedDepth = function(){
                var $v = $('#JQ_buriedDepth'), v = $v.val();
                if(v){
                    if(isNaN(v)){
                        $v.TIP({content:'埋深须为数字！'});
                        data.mode = false;
                    }
                    return v;
                }
                return '';
            }();
            // 时钟位置
            data.clock = function(){
                var $v = $('#JQ_clock'), v = $v.val();
                if(v){
                    var a = v.split(':');
                    if(a.length == 2 && a[0] && a[1]){
                        if(a[0] == '00'){
                            if(isNaN(a[1])){
                                $v.TIP({content: '时钟位置min须为数字!'});
                                data.mode = false;
                            }else{
                                if(!(a[1] >= 0 && a[1] <= 59)){
                                    $v.TIP({
                                        content: '时钟位置min范围0~59!'
                                    });
                                    data.mode = false;
                                }
                            }
                        }else{
                            if(isNaN(a[0])){
                                $v.TIP({content: '时钟位置h须为数字!'});
                                data.mode = false;
                            }else{
                                if(a[0] >= 0 && a[0] <= 12){
                                    if(a[0] == 12){
                                        if(a[1] > 0){
                                            $v.TIP({
                                                content: '时钟位置范围00:00~12:00!'
                                            });
                                            data.mode = false;
                                        }
                                    }
                                }else{
                                    $v.TIP({
                                        content: '时钟位置h范围1~12!'
                                    });
                                    data.mode = false;
                                }
                            }
                            if(isNaN(a[1])){
                                $v.TIP({content: '时钟位置min须为数字!'});
                                data.mode = false;
                            }else{
                                if(!(a[1] >= 0 && a[1] <= 59)){
                                    $v.TIP({
                                        content: '时钟位置min范围0~59!'
                                    });
                                    data.mode = false;
                                }
                            }
                        }
                    }else{
                        $v.TIP({
                            content: '时钟位置格式不正确！'
                        });
                        data.mode = false;
                    }
                    return v;
                }
                return '';
            }();
            // 修复方法
            data.repairType = function(){
                var v = $repair.val();
                if(v){
                    return v;
                }else{
                    $repair.element.TIP({content:'修复方法不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            // 发现时间
            data.foundTime = function(){
                var v = $date.val();
                if(v){
                    return v;
                }else{
                    $date.TIP({content:'发现时间不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            // 备注
            data.createRemark = function(){
                var v = $('#JQ_create_mark').val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 照片
            data.att = function(){
                var $label = $('span.zy-noninternal-span-img');
                var arr = [];
                if($label.length > 0){
                    $label.each(function(){
                        var $this_label = $(this);
                        arr.push($this_label.attr('data-id'));
                    });
                }
                return arr;
            }();

            return DyyBase.convertToArr(data);
        }
    };

    // 上传图片
    var upcount = 0;    // 已上传图片数
    $('button.zy-noninternal-upload-button').each(function(){
        var $this_button = $(this);
        var param = $this_button.attr('data-type');
        var dom_btn = $this_button.get(0);

        // 实例化一个plupload上传对象
        var uploader = new plupload.Uploader({
            browse_button: dom_btn,
            url: DyyBase.url_upload_file(),
            filters: {
                mime_types: [
                    {title:'Image files', extensions: 'jpg,jpeg,gif,png'}
                ]
            },
            multipart_params: {
                type: param
            },
            flash_swf_url: DyyBase.root_path+'/static/js/plugin/plupload/Moxie.swf',
            silverlight_xap_url: DyyBase.root_path+'/static/js/plugin/plupload/Moxie.xap',
            init: {
                BeforeUpload: function(u,f){},
                UploadProgress: function(u,f){},
                FileUploaded: function(u,f,info){
                    showUploadImg(info,$this_button);
                },
                UploadComplete: function(u,f){},
                Error: function(u,f){
                    if(f.code == -601){
                        var msg = f.file.name + '&nbsp;&nbsp;文件格式不正确!<p>请上传 (jpg,jpeg,gif,png) 格式图片!</p>';
                        $this_button.MSG({
                            type: 'error',
                            content: msg,
                            contentTitle: f.file.name,
                            contentAddon: '&nbsp;&nbsp;文件格式不正确!',
                            contentMust: '<p>请上传 (jpg,jpeg,gif,png) 格式图片</p>'
                        });
                    }
                },
                FilesAdded: function(u,f){
                    // 移除列队中的文件
                    function RF(u,f){
                        for(var j = 0, len = f.length; j < len; j++){
                            u.removeFile(f[j]);
                        }
                    }
                    if(f.length > 8){
                        RF(u,f);
                        $this_button.MSG({
                            type: 'error',
                            content: '上传图片数量不超过8张！'
                        });
                    }else{
                        upcount = upcount + f.length;
                        if(upcount > 8){
                            RF(u,f); upcount = upcount - f.length;
                            $this_button.MSG({
                                type: 'error',
                                content: '上传图片数量不超过8张！<br>您已上传 '+upcount+' 张'
                            });
                        }else{
                            uploader.start();
                        }
                    }
                }
            }
        });

        // 初始化
        uploader.init();
    });

    // 为删除照片按钮添加代理事件
    $('label.zy-noninternal-upload-label').on('click','i',function(event){
        var $this_i = $(this);
        var attId = $this_i.attr('data-id');
        DyyBase.ajax(defectApi.url_external_delete_img(),{attId:attId},function(dataset){
            if(dataset.bool){
                upcount = upcount - 1;
                $this_i.parent('span.zy-noninternal-span-img').remove();
            }
        });
        event.stopPropagation();
    });

    // 添加展示上传完成后的图片
    function showUploadImg(o,btn){
        var json = $.parseJSON(o.response);
        var path = DyyBase.file_path+json.result.path;
        var $span = $('<span class="zy-noninternal-span-img" data-path="'+path+'" data-id="'+json.result.id+'"><i data-id="'+json.result.id+'" title="删除照片">×</i><img src="'+path+'"></span>');
        var $label = btn.siblings('label.zy-noninternal-upload-label');
        $label.append($span);
    }

    // 返回
    $('#JQ_create_goback').click(function(){
        window.location.href = DyyBase.root_path + '/pbf/external/externalInspection.html';
    });

    // 保存 - 创建
    $('#JQ_create_submit').click(function(){
        // 验证
        var $form = $('#JQ_create_form');
        var bool = $form.verify();
        if(bool){
            var param = filter.getCondition();
            if(param[0].value){
                DyyBase.ajax(defectApi.url_external_create(),param,function(dataset){
                    if(dataset.bool){
                        $(this).MSG({
                            type: 'success',
                            content: '添加非内检缺陷成功！',
                            yesBtnAct: DyyBase.goToPage,
                            closeBtnAct: DyyBase.goToPage,
                            goToUrl: DyyBase.root_path+'/pbf/external/externalInspection.html'
                        });
                    }
                })
            }
        }
    });

})(jQuery,window,document);